package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 */
@Service
@SuppressWarnings("all")
public class ItemServiceImpl implements ItemService {

    //远程调用service-product-client
    @Autowired
    private ProductFeignClient productFeignClient;


    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private ThreadPoolExecutor executor;

    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 获取详情页数据
     */
    @Override
    public Map<String, Object> getItem(Long skuId) {

        Map<String, Object> resultMap = new HashMap<>();

//        //判断-布隆过滤器
//        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
//        //判断
//        if(!bloomFilter.contains(skuId)){
//            return resultMap;
//        }




        //查询skuInfo信息
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {

            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            resultMap.put("skuInfo",skuInfo);

            return skuInfo;
        },executor);

        //查询销售属性和选中关系
        CompletableFuture<Void> spuSaleAttrListFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {

            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());

            resultMap.put("spuSaleAttrList",spuSaleAttrList);

        },executor);

        //查询三级分类数据
        CompletableFuture<Void> categoryViewFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {

            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            resultMap.put("categoryView",categoryView);

        },executor);

        //查询海报信息
        CompletableFuture<Void> spuPosterListFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {

            List<SpuPoster> spuPosterBySpuId = productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId());
            resultMap.put("spuPosterList",spuPosterBySpuId);


        }, executor);

        //查询切换关系
        CompletableFuture<Void> skuValueIdsMapFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {

            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            resultMap.put("valuesSkuJson", JSON.toJSONString(skuValueIdsMap));


        }, executor);

        //查询实时价格
        CompletableFuture<Void> skuPriceFuture = CompletableFuture.runAsync(() -> {

            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            resultMap.put("price",skuPrice);

        },executor);

        CompletableFuture<Void> attrListFuture = CompletableFuture.runAsync(() ->{

            //查询平台属性 List<Map>
            List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);

            //处理结果
            List<Map<String,String>> attrMapList = attrList.stream().map(baseAttrInfo -> {
                Map<String, String> attrListMap = new HashMap<>();
                attrListMap.put("attrName",baseAttrInfo.getAttrName());
                attrListMap.put("attrValue",baseAttrInfo.getAttrValueList().get(0).getValueName());

                return attrListMap;

            }).collect(Collectors.toList());

            resultMap.put("skuAttrList",attrMapList);


        }, executor);



        //统计商品热度
        CompletableFuture<Void> incrHotScoreFuture = CompletableFuture.runAsync(() -> {

            listFeignClient.incrHotScore(skuId);

        }, executor);


        //异步组合
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                attrListFuture,
                skuValueIdsMapFuture,
                skuPriceFuture,
                spuPosterListFuture,
                categoryViewFuture,
                spuSaleAttrListFuture,
                incrHotScoreFuture
        ).join();

        return resultMap;

/*        //查询skuInfo信息
        SkuInfo skuInfo= productFeignClient.getSkuInfo(skuId);
        resultMap.put("skuInfo",skuInfo);

        if (skuInfo != null) {
            //查询销售属性和选中关系
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            resultMap.put("spuSaleAttrList",spuSaleAttrList);

            //查询三级分类数据
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            resultMap.put("categoryView",categoryView);
            //查询海报信息
            List<SpuPoster> spuPosterBySpuId = productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId());
            resultMap.put("spuPosterList",spuPosterBySpuId);
            //查询切换关系
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            resultMap.put("valuesSkuJson", JSON.toJSONString(skuValueIdsMap));

        }

        //查询实时价格
        BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
        resultMap.put("price",skuPrice);


        //查询平台属性 List<Map>
        List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
        //处理结果
        List<Map<String, String>> attrMapList = attrList.stream().map(baseAttrInfo -> {
            Map<String, String> attrListMap = new HashMap<>();
            attrListMap.put("attrName", baseAttrInfo.getAttrName());
            attrListMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());

            return attrListMap;
        }).collect(Collectors.toList());


        resultMap.put("skuAttrList",attrMapList);

        return resultMap;
    }*/
    }
}